<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-28 19:51:46
 * @LastEditTime: 2023-11-30 11:47:52
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    title='新建数据'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <div style='max-height: 500px;overflow: auto'>
      <a-spin :spinning='loading'>
        <a-form :form='form' v-bind='formLayout'>
          <a-form-item label='设备名称'>
            <a-input placeholder='请输入设备名称'
                     v-decorator="['deviceName',{ rules: [
                       { required: true, message: '请输入设备名称!' },
                        { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}
                       ] }]" />
          </a-form-item>
          <a-form-item label='设备编号'>
            <a-input placeholder='请输入设备编号'
                     v-decorator="['deviceNo',{ rules: [
                       { required: true, message: '请输入设备编号!' },
                       { validator: validaNumber}
                       ] }]" />
          </a-form-item>
          <a-form-item label='设备目录'>
            <a-input placeholder='请输入设备目录' v-decorator="['deviceDirectory']" />
          </a-form-item>
          <a-form-item label='设备型号'>
            <a-input placeholder='请输入设备型号' v-decorator="['deviceType']" />
          </a-form-item>
          <a-form-item label='安装位置'>
            <a-input placeholder='请选择安装位置' v-decorator="['installLocation']" />
          </a-form-item>
          <a-form-item label='安装日期'>
            <a-date-picker style='width: 100%;' placeholder='请选择安装日期' v-decorator="['installDate']"
                           valueFormat='YYYY-MM-DD' />
          </a-form-item>
          <a-form-item label='投运日期'>
            <a-date-picker style='width: 100%;' placeholder='请选择投运日期' v-decorator="['useDate']"
                           valueFormat='YYYY-MM-DD' />
          </a-form-item>
          <a-form-item label='生产厂家'>
            <a-input placeholder='请输入生产厂家' v-decorator="['producer']" />
          </a-form-item>
          <a-form-item label='厂家联系人'>
            <a-input placeholder='请输入厂家联系人' v-decorator="['contactPerson']" />
          </a-form-item>
          <a-form-item label='厂家联系电话'>
            <a-input placeholder='请输入厂家联系电话'
                     v-decorator="['producerPhone',{rules: [{ required: true, message: '请输入联系电话' }, { pattern: phonePattern,  message: '请输入合法电话'}], validateTrigger: 'blur'}]" />
          </a-form-item>
          <!--          <a-form-item label='入库时间'>-->
          <!--            <a-date-picker style='width: 100%;' placeholder='请选择入库日期' v-decorator="['createdTime']"-->
          <!--                           valueFormat='YYYY-MM-DD' />-->
          <!--          </a-form-item>-->
        </a-form>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { namePattern, phonePattern } from '@/reges'
// 表单字段
const fields = ['deviceName', 'deviceNo', 'deviceDirectory', 'deviceType', 'installLocation', 'installDate', 'useDate', 'producer', 'contactPerson', 'producerPhone', 'createdTime', 'pageType']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      namePattern,
      form: this.$form.createForm(this),
      phonePattern
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    // /^[A-Za-z0-9]+$/
    validatorPhone(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入设备名称'))
      } else {
        if (!/^\S*$/.test(value)) {
          callback(new Error('请勿输入空格等特殊字符!'))
        } else {
          callback()
        }
      }
    },
    validaNumber(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入设备编号'))
      } else {
        if (!/^[A-Za-z0-9]+$/.test(value)) {
          callback(new Error('请输入字母加数字组合!'))
        } else {
          callback()
        }
      }
    }
  }
}
</script>
